﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace zmMoniter
{
    class NonVibrators
    {
        public string sID = "";
        public string sName = "";
        public int iColor;
        public double dArea = -1;
        public string sDisc = "";
        public double dCenterX;
        public double dCenterY;
        public Extent extent;
        public ArrayList alPlgGraph = null;
        GraphicsPath myGraphicsPath;
        Region myRegion;
        public bool bIsAddedPoint = false;
        public  NonVibrators()
        {
            alPlgGraph=new ArrayList();//非振捣区域临时点位数组
            myGraphicsPath = new GraphicsPath();
            myRegion = new Region();
            myGraphicsPath.Reset();
            extent = new Extent();
        }
        public void addPoint(Point pt)
        {
            alPlgGraph.Add(pt);
            extent.upDate(pt);
        }
        public string getNonVibrPoints()
        {
            string sResult = "";
            for (int i = 0; i < alPlgGraph.Count; i++)
            {
                Point pt = (Point)alPlgGraph[i];
                if (i == alPlgGraph.Count - 1)
                    sResult += pt.getX() + "," + pt.getY();
                else
                    sResult += pt.getX() + "," + pt.getY() + ";";
            }
            return sResult;
        }
        public double getPointX(int index)
        {
            if (alPlgGraph.Count > index && index >= 0)
            {
                return ((Point)alPlgGraph[index]).dX;
            }
            else
                return -9999;
        }
        public double getPointY(int index)
        {
            if (alPlgGraph.Count > index && index >= 0)
            {
                return ((Point)alPlgGraph[index]).dY;
            }
            else
                return -9999;
        }
        public void setRegion()
        {
           
            if (!bIsAddedPoint)
            {
                if (alPlgGraph.Count < 2)
                    return;

                Point pt1 = (Point)alPlgGraph[0];
                Point ptn = (Point)alPlgGraph[alPlgGraph.Count - 1];
                if (!pt1.compare(ptn))
                {
                    addPoint(pt1);
                }

                int iLength = alPlgGraph.Count;
                System.Drawing.PointF[] pts = new System.Drawing.PointF[iLength];
                for (int j = 0; j < iLength; j++)
                {
                    Point ptj = (Point)alPlgGraph[j];
                    pts[j] = new PointF((float)ptj.dX, (float)ptj.dY);
                }
                myGraphicsPath.AddPolygon(pts);
                myRegion.MakeEmpty();
                myRegion.Union(myGraphicsPath);
                bIsAddedPoint = true;
            }
        }
        public bool IsIn(double ptX, double ptY, double dLength = 0.1)
        {
            bool bResult = false;
            if (bIsAddedPoint == false)
                return false;
            if (!extent.inExtent(ptX, ptY))
            {
                return false;
            }
            if (IsPointInRegion(ptX, ptY ))
                return true;
            /*
            if (IsPointInRegion(ptX + dLength / 2, ptY + dLength / 2))
                return true;
            if (IsPointInRegion(ptX + dLength / 2, ptY - dLength / 2))
                return true;
            if (IsPointInRegion(ptX - dLength / 2, ptY - dLength / 2))
                return true;
             */
            return bResult;
        }
        public bool IsPointInRegion(double dX, double dY)
        {
            PointF ptf = new PointF((float)dX, (float)dY);
            return myRegion.IsVisible(ptf);
        }
    }
}
